package org.rency.mq.activemq.listener.failover.impl;

import org.rency.mq.activemq.listener.failover.RetryStrategy;
import org.rency.mq.activemq.request.MQRequest;

public class FrequencyRetryStrategy extends AbstractRetryStrategy implements RetryStrategy {
	private int frequency = 10;

	public int getFrequency() {
		return frequency;
	}

	public void setFrequency(int frequency) {
		this.frequency = frequency;
	}

	@Override
	protected boolean needRetry(MQRequest request) {
		if (frequency <= 0)
			return true;

		int failedTimes = 1;
		if (request.containsProperty("failedTimes")){
			failedTimes = (Integer) request.getProperty("failedTimes") + 1;
		}
		
		request.addProperty("failedTimes", failedTimes);

		if(failedTimes  <= frequency){
			return true;
		}

		return false;
	}
	
	
}
